#include <glib.h>

#define BTREE_LEN 3

gboolean _traverse_str_str_func (gpointer key, gpointer value, gpointer data)
{
    g_print("key:%s, value:%s user_data:%s \n", (char *)key, (char *)value, (gchar *)data);
    return 0;
}

void _str_str_key_destroy_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

void _str_str_value_destroy_func(gpointer data)
{
    if(NULL != data) {
        g_free(data);
    }
}

gint gtree_test_str_str_data(void)
{
    GTree *tree = NULL;
    gchar *key[BTREE_LEN] = {NULL};
    gchar *val[BTREE_LEN] = {NULL};
    gint i = 0;

    tree = g_tree_new_full((GCompareDataFunc)strcmp,NULL,_str_str_key_destroy_func,_str_str_value_destroy_func);
    if(NULL == tree) {
        g_print("g_tree_new return NULL \n");
        return 0;
    }

    for(i=0; i<BTREE_LEN; i++) {
        key[i] = g_strdup_printf("key-%d", i);
        val[i] = g_strdup_printf("value-%d", i);
        g_tree_insert(tree, (gpointer)key[i], (gpointer)val[i]);
    }

    g_tree_foreach(tree, _traverse_str_str_func, "str-str");

    g_tree_steal(tree, (gconstpointer)"key-1");

    g_tree_foreach(tree, _traverse_str_str_func, "str-str-after-steal");

    g_print("key[1]:%s, val[1]:%s \n", key[1], val[1]);
    g_free(key[1]);
    g_free(val[1]);

    g_tree_destroy(tree);

    return 0;
}

gint main(gint argc, gchar **argv)
{
    gtree_test_str_str_data();

    return 0;
}